﻿using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{

    public class wnxd_SubjectProduct_Random : WebControl
    {

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder = new StringBuilder();
            int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
            DataTable productList = this.GetProductList(categoryId, 4);
            if ((productList != null) && (productList.Rows.Count > 0))
            {
                foreach (DataRow row in productList.Rows)
                {
                    builder.AppendFormat("<div class=\"content2-box\">");
                    builder.AppendFormat("<div class=\"pro8\">");
                    string title = string.Empty;
                    string alt = string.Empty;
                    string keyword = row["Meta_Keywords"].ToString();
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        string[] arr = keyword.Split('|');
                        if (arr.Length == 3)
                        {
                            title = arr[1];
                            alt = arr[2];
                        }
                    }
                    builder.AppendFormat("<a href=\"{1}\" target=\"_blank\"><img class=\"z-img\" data-original=\"{0}\" title=\"{2}\" alt=\"{3}\" /></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                    builder.AppendFormat("<p><a title=\"{0}\" href=\"{1}\" style=\"color:#333333\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                    if (row["ProductText"] != DBNull.Value) builder.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" title=\"{0}\" href=\"{1}\" target=\"_blank\">{0}</a>", row["ProductText"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                    if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                    builder.AppendFormat("</p><b>￥{0}</b>", Globals.FormatMoney((decimal)row["RankPrice"]));
                    builder.AppendFormat("<span>特价</span></div></div>");
                }
            }
            return builder.ToString();
        }
        private DataTable GetProductList(int categoryid, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "NewID()",
                CategoryIds = categoryid.ToString(),
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }
    }
}